package com.wiscamp.ninechapters.panels.domain.entities;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import cube.ddd.domain.Entity;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import java.time.LocalDateTime;
import java.util.UUID;

@Data
public class Application implements Entity {
    /**
     * 应用编号
     */
    private long applicationId = 0;

    /**
     * 应用代码
     */
    @NotNull
    @NotBlank(message = "应用代码不能为空。")
    @Length(max = 50, message = "应用代码文本长度不能超过50。")
    private String applicationCode = "";

    /**
     * 应用名称
     */
    @NotNull
    @NotBlank(message = "应用名称不能为空。")
    @Length(max = 100, message = "应用名称文本长度不能超过100。")
    private String applicationName = "";

    /**
     * 安全密钥
     */
    @Length(max = 50, message = "安全密钥文本长度不能超过50。")
    private String secretKey = "";

    /**
     * 应用分组
     */
    @Length(max = 50, message = "应用分组文本长度不能超过50。")
    private String applicationGroup = "";

    /**
     * 备注
     */
    @Length(max = 250, message = "备注文本长度不能超过250。")
    private String remark = "";

    /**
     * 创建用户
     */
    private long creatorId = 0L;

    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime creationTime;

    /**
     * 编辑用户
     */
    private long modifierId = 0L;

    /**
     * 编辑时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime modificationTime;

    /**
     * 是否启用
     */
    private boolean enabled = false;

    /**
     * 是否删除
     */
    private boolean deleted = false;

    /**
     * 创建人名称
     */
    private String creatorName = "";

    /**
     * 编辑人名称
     */
    private String modifierName = "";

    public void initSecretKey() {
        if ("".equalsIgnoreCase(this.getSecretKey()))
            this.secretKey = UUID.randomUUID().toString();
    }

    public void enable() {
        if (!this.enabled) {
            this.enabled = true;
        }
    }

    public void disable() {
        if (this.enabled) {
            this.enabled = false;
        }
    }

    public void delete() {
        if (!this.deleted) {
            this.deleted = true;
        }
    }

    public void createBy(long operatorId) {
        this.creatorId = operatorId;
        this.creationTime = LocalDateTime.now();
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public void updateBy(long operatorId) {
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public Application(String appCode, String appName) {
        this.setApplicationCode(appCode);
        this.setApplicationName(appName);
    }
}
